/
DeploymentLoader.java
2166 lines (1849 loc) · 94.9 KB
/
DeploymentLoader.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.openejb.config;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.openejb.ClassLoaderUtil;
import org.apache.openejb.OpenEJBException;
import org.apache.openejb.api.LocalClient;
import org.apache.openejb.api.RemoteClient;
import org.apache.openejb.cdi.CompositeBeans;
import org.apache.openejb.classloader.ClassLoaderConfigurer;
import org.apache.openejb.classloader.WebAppEnricher;
import org.apache.openejb.config.event.BeforeDeploymentEvent;
import org.apache.openejb.config.sys.Resources;
import org.apache.openejb.core.EmptyResourcesClassLoader;
import org.apache.openejb.core.ParentClassLoaderFinder;
import org.apache.openejb.jee.Application;
import org.apache.openejb.jee.ApplicationClient;
import org.apache.openejb.jee.Beans;
import org.apache.openejb.jee.Connector;
import org.apache.openejb.jee.EjbJar;
import org.apache.openejb.jee.FacesConfig;
import org.apache.openejb.jee.JavaWsdlMapping;
import org.apache.openejb.jee.JspConfig;
import org.apache.openejb.jee.Module;
import org.apache.openejb.jee.Taglib;
import org.apache.openejb.jee.TldTaglib;
import org.apache.openejb.jee.WebApp;
import org.apache.openejb.jee.WebserviceDescription;
import org.apache.openejb.jee.Webservices;
import org.apache.openejb.jee.oejb3.OpenejbJar;
import org.apache.openejb.loader.FileUtils;
import org.apache.openejb.loader.IO;
import org.apache.openejb.loader.SystemInstance;
import org.apache.openejb.sxc.ApplicationXml;
import org.apache.openejb.util.AnnotationFinder;
import org.apache.openejb.util.JarExtractor;
import org.apache.openejb.util.JavaSecurityManagers;
import org.apache.openejb.util.LogCategory;
import org.apache.openejb.util.Logger;
import org.apache.openejb.util.URLs;
import org.apache.xbean.finder.IAnnotationFinder;
import org.apache.xbean.finder.ResourceFinder;
import org.apache.xbean.finder.UrlSet;
import org.apache.xbean.finder.archive.ClassesArchive;
import org.apache.xbean.finder.filter.Filter;
import org.apache.xbean.finder.filter.Filters;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.Locale;
import static java.util.Arrays.asList;
/**
* @version $Revision$ $Date$
*/
public class DeploymentLoader implements DeploymentFilterable {
public static final Logger logger = Logger.getInstance(LogCategory.OPENEJB_STARTUP_CONFIG, "org.apache.openejb.util.resources");
public static final String OPENEJB_ALTDD_PREFIX = "openejb.altdd.prefix";
private static final String ddDir = "META-INF/";
public static final String EAR_WEBAPP_PERSISTENCE_XML_JARS = "ear-webapp-persistence-xml-jars";
public static final String EAR_SCOPED_CDI_BEANS = "ear-scoped-cdi-beans_";
public static final String RAR_URLS_KEY = "rar-urls";
public static final String URLS_KEY = "urls";
private static final String RESOURCES_XML = "resources.xml";
private static final String WEB_FRAGMENT_XML = "web-fragment.xml";
private final boolean scanManagedBeans = true;
private static final Collection<String> KNOWN_DESCRIPTORS = Arrays.asList("app-ctx.xml", "module.properties", "application.properties", "web.xml", "ejb-jar.xml", "openejb-jar.xml", "env-entries.properties", "beans.xml", "ra.xml", "application.xml", "application-client.xml", "persistence-fragment.xml", "persistence.xml", "validation.xml", NewLoaderLogic.EXCLUSION_FILE);
private static String ALTDD = SystemInstance.get().getOptions().get(OPENEJB_ALTDD_PREFIX, (String) null);
private volatile List<URL> containerUrls = null;
@Deprecated // use load(File, ExternalConfiguration)
public AppModule load(final File jarFile) throws OpenEJBException {
return load(jarFile, null);
}
/**
* @param jarFile the app file (war, jar, ear)
* @param config potentially some more config, mainly used when linking to another system like tomcat to enrich the conf we can guess
* @return the loaded module
*/
public AppModule load(final File jarFile, final ExternalConfiguration config) throws OpenEJBException {
// verify we have a valid file
final String jarPath;
try {
jarPath = jarFile.getCanonicalPath();
} catch (final IOException e) {
throw new OpenEJBException("Invalid application file path " + jarFile, e);
}
final URL baseUrl = getFileUrl(jarFile);
// create a class loader to use for detection of module type
// do not use this class loader for any other purposes... it is
// non-temp class loader and usage will mess up JPA
ClassLoader doNotUseClassLoader = null;// = ClassLoaderUtil.createClassLoader(jarPath, new URL[]{baseUrl}, OpenEJB.class.getClassLoader());
try {
// determine the module type
final Class<? extends DeploymentModule> moduleClass;
try {
doNotUseClassLoader = ClassLoaderUtil.createClassLoader(jarPath, new URL[]{baseUrl}, getOpenEJBClassLoader());
moduleClass = discoverModuleType(baseUrl, ClassLoaderUtil.createTempClassLoader(doNotUseClassLoader), true);
} catch (final Exception e) {
throw new UnknownModuleTypeException("Unable to determine module type for jar: " + baseUrl.toExternalForm(), e);
}
if (ResourcesModule.class.equals(moduleClass)) {
final AppModule appModule = new AppModule(null, jarPath);
final ResourcesModule module = new ResourcesModule();
module.getAltDDs().put(RESOURCES_XML, baseUrl);
ReadDescriptors.readResourcesXml(module);
module.initAppModule(appModule);
// here module is no more useful since everything is in the appmodule
return appModule;
}
//We always load AppModule, as it somewhat likes a wrapper module
if (AppModule.class.equals(moduleClass)) {
return createAppModule(jarFile, jarPath);
}
if (EjbModule.class.equals(moduleClass)) {
final URL[] urls = new URL[]{baseUrl};
SystemInstance.get().fireEvent(new BeforeDeploymentEvent(urls));
final ClassLoader classLoader = ClassLoaderUtil.createTempClassLoader(jarPath, urls, getOpenEJBClassLoader());
final AppModule appModule;
//final Class<? extends DeploymentModule> o = EjbModule.class;
final EjbModule ejbModule = createEjbModule(baseUrl, jarPath, classLoader);
// wrap the EJB Module with an Application Module
appModule = new AppModule(ejbModule);
addPersistenceUnits(appModule, baseUrl);
return appModule;
}
if (ClientModule.class.equals(moduleClass)) {
final String jarLocation = URLs.toFilePath(baseUrl);
final ClientModule clientModule = createClientModule(baseUrl, jarLocation, getOpenEJBClassLoader(), null);
// Wrap the resource module with an Application Module
return new AppModule(clientModule);
}
if (ConnectorModule.class.equals(moduleClass)) {
final String jarLocation = URLs.toFilePath(baseUrl);
final ConnectorModule connectorModule = createConnectorModule(jarLocation, jarLocation, getOpenEJBClassLoader(), null);
if (connectorModule != null) {
final List<ConnectorModule> connectorModules = new ArrayList<ConnectorModule>();
// let it be able to deploy the same connector several times
final String id = connectorModule.getModuleId();
if (!"true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.connector." + id + ".skip-default", "false"))) {
connectorModules.add(connectorModule);
}
final String aliases = SystemInstance.get().getProperty("openejb.connector." + id + ".aliases");
if (aliases != null) {
for (final String alias : aliases.split(",")) {
final ConnectorModule aliasModule = createConnectorModule(jarLocation, jarLocation, getOpenEJBClassLoader(), alias);
connectorModules.add(aliasModule);
}
}
// Wrap the resource module with an Application Module
final AppModule appModule = new AppModule(connectorModules.toArray(new ConnectorModule[connectorModules.size()]));
return appModule;
}
}
if (WebModule.class.equals(moduleClass)) {
final File file = URLs.toFile(baseUrl);
// Standalone Web Module
final WebModule webModule = createWebModule(file.getAbsolutePath(), baseUrl, getOpenEJBClassLoader(), getContextRoot(), getModuleName(), config);
// important to use the webapp classloader here otherwise each time we'll check something using loadclass it will fail (=== empty classloader)
final AppModule appModule = new AppModule(webModule.getClassLoader(), file.getAbsolutePath(), new Application(), true);
addWebModule(webModule, appModule);
addWebModuleDescriptors(baseUrl, webModule, appModule);
appModule.setStandloneWebModule();
appModule.setDelegateFirst(true); // force it for webapps
return appModule;
}
if (PersistenceModule.class.equals(moduleClass)) {
final String jarLocation = URLs.toFilePath(baseUrl);
final ClassLoader classLoader = ClassLoaderUtil.createTempClassLoader(jarPath, new URL[]{baseUrl}, getOpenEJBClassLoader());
// wrap the EJB Module with an Application Module
final AppModule appModule = new AppModule(classLoader, jarLocation);
// Persistence Units
addPersistenceUnits(appModule, baseUrl);
return appModule;
}
throw new UnsupportedModuleTypeException("Unsupported module type: " + moduleClass.getSimpleName());
} finally {
// if the application was unpacked appId used to create this class loader will be wrong
// We can safely destroy this class loader in either case, as it was not use by any modules
if (null != doNotUseClassLoader) {
ClassLoaderUtil.destroyClassLoader(doNotUseClassLoader);
}
}
}
public static void addWebModuleDescriptors(final URL baseUrl, final WebModule webModule, final AppModule appModule) throws OpenEJBException {
final Map<String, Object> otherDD = new HashMap<String, Object>();
final List<URL> urls = webModule.getScannableUrls();
final ResourceFinder finder = new ResourceFinder("", urls.toArray(new URL[urls.size()]));
otherDD.putAll(getDescriptors(finder, false));
// "persistence.xml" is done separately since we manage a list of url and not s single url
try {
final List<URL> persistenceXmls = finder.findAll(ddDir + "persistence.xml");
if (persistenceXmls.size() >= 1) {
final URL old = (URL) otherDD.get("persistence.xml");
if (old != null && !persistenceXmls.contains(old)) {
persistenceXmls.add(old);
}
otherDD.put("persistence.xml", persistenceXmls);
}
} catch (final IOException e) {
// ignored
}
addConnectorModules(appModule, webModule);
addWebPersistenceDD("persistence.xml", otherDD, appModule);
addWebPersistenceDD("persistence-fragment.xml", otherDD, appModule);
addPersistenceUnits(appModule, baseUrl);
addWebFragments(webModule, urls);
}
private static void addConnectorModules(final AppModule appModule, final WebModule webModule) throws OpenEJBException {
// WEB-INF
if (webModule.getAltDDs().containsKey("ra.xml")) {
final String jarLocation = new File(webModule.getJarLocation(), "/WEB-INF/classes").getAbsolutePath();
final ConnectorModule connectorModule = createConnectorModule(jarLocation, jarLocation, webModule.getClassLoader(), webModule.getModuleId() + "RA", (URL) webModule.getAltDDs().get("ra.xml"));
if (connectorModule != null) {
appModule.getConnectorModules().add(connectorModule);
}
}
// .rar
for (final URL url : webModule.getRarUrls()) {
try {
final File file = URLs.toFile(url);
if (file.getName().endsWith(".rar")) {
final String jarLocation = file.getAbsolutePath();
final ConnectorModule connectorModule = createConnectorModule(jarLocation, jarLocation, webModule.getClassLoader(), null);
if (connectorModule != null) {
appModule.getConnectorModules().add(connectorModule);
}
}
} catch (final Exception e) {
logger.error("error processing url " + url.toExternalForm(), e);
}
}
for (final URL url : webModule.getScannableUrls()) {
try {
final File file = URLs.toFile(url);
if (file.getName().endsWith(".jar")) {
try (JarFile jarFile = new JarFile(file)) {
// TODO: better management of altdd
String name = (ALTDD != null ? ALTDD + "." : "") + "ra.xml";
JarEntry entry = jarFile.getJarEntry(name);
if (entry == null) {
name = "META-INF/" + name;
entry = jarFile.getJarEntry(name);
}
if (entry == null) {
continue;
}
final String jarLocation = file.getAbsolutePath();
final ConnectorModule connectorModule = createConnectorModule(jarLocation, jarLocation, webModule.getClassLoader(), null);
if (connectorModule != null) {
appModule.getConnectorModules().add(connectorModule);
}
}
}
} catch (final Exception e) {
logger.error("error processing url " + url.toExternalForm(), e);
}
}
}
protected ClassLoader getOpenEJBClassLoader() {
return ParentClassLoaderFinder.Helper.get();
}
@SuppressWarnings("unchecked")
private static void addWebPersistenceDD(final String name, final Map<String, Object> otherDD, final AppModule appModule) {
if (otherDD.containsKey(name)) {
List<URL> persistenceUrls = (List<URL>) appModule.getAltDDs().get(name);
if (persistenceUrls == null) {
persistenceUrls = new ArrayList<URL>();
appModule.getAltDDs().put(name, persistenceUrls);
}
if (otherDD.containsKey(name)) {
final Object otherUrl = otherDD.get(name);
if (otherUrl instanceof URL && !persistenceUrls.contains(otherUrl)) {
persistenceUrls.add((URL) otherUrl);
} else if (otherUrl instanceof List) {
final List<URL> otherList = (List<URL>) otherDD.get(name);
for (final URL url : otherList) {
if (!persistenceUrls.contains(url)) {
persistenceUrls.add(url);
}
}
}
}
}
}
protected AppModule createAppModule(final File jarFile, final String jarPath) throws OpenEJBException {
File appDir = unpack(jarFile);
try {
appDir = appDir.getCanonicalFile();
} catch (final IOException e) {
throw new OpenEJBException("Invalid application directory " + appDir.getAbsolutePath());
}
final URL appUrl = getFileUrl(appDir);
final String appId = appDir.getAbsolutePath();
final ClassLoader tmpClassLoader = ClassLoaderUtil.createTempClassLoader(appId, new URL[]{appUrl}, getOpenEJBClassLoader());
final ResourceFinder finder = new ResourceFinder("", tmpClassLoader, appUrl);
final Map<String, URL> appDescriptors = getDescriptors(finder);
try {
//
// Find all the modules using either the application xml or by searching for all .jar, .war and .rar files.
//
final Map<String, URL> ejbModules = new LinkedHashMap<String, URL>();
final Map<String, URL> clientModules = new LinkedHashMap<String, URL>();
final Map<String, URL> resouceModules = new LinkedHashMap<String, URL>();
final Map<String, URL> webModules = new LinkedHashMap<String, URL>();
final Map<String, String> webContextRoots = new LinkedHashMap<String, String>();
final URL applicationXmlUrl = appDescriptors.get("application.xml");
final List<URL> extraLibs = new ArrayList<URL>();
final Application application;
if (applicationXmlUrl != null) {
application = unmarshal(applicationXmlUrl);
for (final Module module : application.getModule()) {
try {
if (module.getEjb() != null) {
final URL url = finder.find(module.getEjb().trim());
ejbModules.put(module.getEjb(), url);
} else if (module.getJava() != null) {
final URL url = finder.find(module.getJava().trim());
clientModules.put(module.getJava(), url);
extraLibs.add(url);
} else if (module.getConnector() != null) {
final URL url = finder.find(module.getConnector().trim());
resouceModules.put(module.getConnector(), url);
} else if (module.getWeb() != null) {
final URL url = finder.find(module.getWeb().getWebUri().trim());
webModules.put(module.getWeb().getWebUri(), url);
webContextRoots.put(module.getWeb().getWebUri(), module.getWeb().getContextRoot());
}
} catch (final IOException e) {
throw new OpenEJBException("Invalid path to module " + e.getMessage(), e);
}
}
} else {
application = new Application();
final HashMap<String, URL> files = new HashMap<String, URL>();
scanDir(appDir, files, "", false);
files.remove("META-INF/MANIFEST.MF");
// todo we should also filter URLs here using DeploymentsResolver.loadFromClasspath
createApplicationFromFiles(appId, tmpClassLoader, ejbModules, clientModules, resouceModules, webModules, files);
}
final ClassLoaderConfigurer configurer = QuickJarsTxtParser.parse(new File(appDir, "META-INF/" + QuickJarsTxtParser.FILE_NAME));
final Collection<URL> jarsXmlLib = new ArrayList<>();
if (configurer != null) {
for (final URL url : configurer.additionalURLs()) {
try {
detectAndAddModuleToApplication(appId, tmpClassLoader,
ejbModules, clientModules, resouceModules, webModules,
new ImmutablePair<>(URLs.toFile(url).getAbsolutePath(), url));
} catch (final Exception e) {
jarsXmlLib.add(url);
}
}
}
//
// Create a class loader for the application
//
// lib/*
if (application.getLibraryDirectory() == null) {
application.setLibraryDirectory("lib/");
} else {
final String dir = application.getLibraryDirectory();
if (!dir.endsWith("/")) {
application.setLibraryDirectory(dir + "/");
}
}
try {
final Map<String, URL> libs = finder.getResourcesMap(application.getLibraryDirectory());
extraLibs.addAll(libs.values());
} catch (final IOException e) {
logger.warning("Cannot load libs from '" + application.getLibraryDirectory() + "' : " + e.getMessage(), e);
}
// APP-INF/lib/*
try {
final Map<String, URL> libs = finder.getResourcesMap("APP-INF/lib/");
extraLibs.addAll(libs.values());
} catch (final IOException e) {
logger.warning("Cannot load libs from 'APP-INF/lib/' : " + e.getMessage(), e);
}
// META-INF/lib/*
try {
final Map<String, URL> libs = finder.getResourcesMap("META-INF/lib/");
extraLibs.addAll(libs.values());
} catch (final IOException e) {
logger.warning("Cannot load libs from 'META-INF/lib/' : " + e.getMessage(), e);
}
// All jars nested in the Resource Adapter
final HashMap<String, URL> rarLibs = new HashMap<String, URL>();
for (final Map.Entry<String, URL> entry : resouceModules.entrySet()) {
try {
// unpack the resource adapter archive
File rarFile = URLs.toFile(entry.getValue());
rarFile = unpack(rarFile);
entry.setValue(rarFile.toURI().toURL());
scanDir(appDir, rarLibs, "");
} catch (final MalformedURLException e) {
throw new OpenEJBException("Malformed URL to app. " + e.getMessage(), e);
}
}
for (final Iterator<Map.Entry<String, URL>> iterator = rarLibs.entrySet().iterator(); iterator.hasNext(); ) {
// remove all non jars from the rarLibs
final Map.Entry<String, URL> fileEntry = iterator.next();
if (!fileEntry.getKey().endsWith(".jar")) {
continue;
}
iterator.remove();
}
final List<URL> classPath = new ArrayList<>();
classPath.addAll(ejbModules.values());
classPath.addAll(clientModules.values());
classPath.addAll(rarLibs.values());
classPath.addAll(extraLibs);
classPath.addAll(jarsXmlLib);
final URL[] urls = classPath.toArray(new URL[classPath.size()]);
SystemInstance.get().fireEvent(new BeforeDeploymentEvent(urls));
final ClassLoader appClassLoader = ClassLoaderUtil.createTempClassLoader(appId, urls, getOpenEJBClassLoader());
//
// Create the AppModule and all nested module objects
//
final AppModule appModule = new AppModule(appClassLoader, appId, application, false);
appModule.getAdditionalLibraries().addAll(extraLibs);
appModule.getAltDDs().putAll(appDescriptors);
appModule.getWatchedResources().add(appId);
if (applicationXmlUrl != null) {
appModule.getWatchedResources().add(URLs.toFilePath(applicationXmlUrl));
}
if (appDescriptors.containsKey(RESOURCES_XML)) {
final Map<String, Object> altDd = new HashMap<String, Object>(appDescriptors);
ReadDescriptors.readResourcesXml(new org.apache.openejb.config.Module(false) {
@Override
public Map<String, Object> getAltDDs() {
return altDd;
}
@Override
public void initResources(final Resources resources) {
appModule.getContainers().addAll(resources.getContainer());
appModule.getResources().addAll(resources.getResource());
appModule.getServices().addAll(resources.getService());
}
});
}
// EJB modules
for (final String moduleName : ejbModules.keySet()) {
try {
URL ejbUrl = ejbModules.get(moduleName);
// we should try to use a reference to the temp classloader
if (ClassLoaderUtil.isUrlCached(appModule.getJarLocation(), ejbUrl)) {
try {
ejbUrl = ClassLoaderUtil.getUrlCachedName(appModule.getJarLocation(), ejbUrl).toURI().toURL();
} catch (final MalformedURLException ignore) {
// no-op
}
}
final File ejbFile = URLs.toFile(ejbUrl);
final String absolutePath = ejbFile.getAbsolutePath();
final EjbModule ejbModule = createEjbModule(ejbUrl, absolutePath, appClassLoader);
appModule.getEjbModules().add(ejbModule);
} catch (final OpenEJBException e) {
logger.error("Unable to load EJBs from EAR: " + appId + ", module: " + moduleName + ". Exception: " + e.getMessage(), e);
}
}
// Application Client Modules
for (final String moduleName : clientModules.keySet()) {
try {
URL clientUrl = clientModules.get(moduleName);
// we should try to use a reference to the temp classloader
if (ClassLoaderUtil.isUrlCached(appModule.getJarLocation(), clientUrl)) {
try {
clientUrl = ClassLoaderUtil.getUrlCachedName(appModule.getJarLocation(), clientUrl).toURI().toURL();
} catch (final MalformedURLException ignore) {
// no-op
}
}
final File clientFile = URLs.toFile(clientUrl);
final String absolutePath = clientFile.getAbsolutePath();
final ClientModule clientModule = createClientModule(clientUrl, absolutePath, appClassLoader, null);
appModule.getClientModules().add(clientModule);
} catch (final Exception e) {
logger.error("Unable to load App Client from EAR: " + appId + ", module: " + moduleName + ". Exception: " + e.getMessage(), e);
}
}
// Resource modules
for (final String moduleName : resouceModules.keySet()) {
try {
URL rarUrl = resouceModules.get(moduleName);
// we should try to use a reference to the temp classloader
if (ClassLoaderUtil.isUrlCached(appModule.getJarLocation(), rarUrl)) {
try {
rarUrl = ClassLoaderUtil.getUrlCachedName(appModule.getJarLocation(), rarUrl).toURI().toURL();
} catch (final MalformedURLException ignore) {
// no-op
}
}
final ConnectorModule connectorModule = createConnectorModule(appId, URLs.toFilePath(rarUrl), appClassLoader, moduleName);
if (connectorModule != null) {
appModule.getConnectorModules().add(connectorModule);
}
} catch (final OpenEJBException e) {
logger.error("Unable to load RAR: " + appId + ", module: " + moduleName + ". Exception: " + e.getMessage(), e);
}
}
// Web modules
for (final String moduleName : webModules.keySet()) {
try {
final URL warUrl = webModules.get(moduleName);
addWebModule(appModule, warUrl, appClassLoader, webContextRoots.get(moduleName), null);
} catch (final OpenEJBException e) {
logger.error("Unable to load WAR: " + appId + ", module: " + moduleName + ". Exception: " + e.getMessage(), e);
}
}
addBeansXmls(appModule);
// Persistence Units
final Properties p = new Properties();
p.put(appModule.getModuleId(), appModule.getJarLocation());
final FileUtils base = new FileUtils(appModule.getModuleId(), appModule.getModuleId(), p);
final List<URL> filteredUrls = new ArrayList<>();
DeploymentsResolver.loadFromClasspath(base, filteredUrls, appModule.getClassLoader());
addPersistenceUnits(appModule, filteredUrls.toArray(new URL[filteredUrls.size()]));
final Object pXmls = appModule.getAltDDs().get("persistence.xml");
for (final WebModule webModule : appModule.getWebModules()) {
final List<URL> foundRootUrls = new ArrayList<>();
final List<URL> scannableUrls = webModule.getScannableUrls();
for (final URL url : scannableUrls) {
if (!addPersistenceUnits(appModule, url).isEmpty()) {
foundRootUrls.add(url);
}
}
if (pXmls != null && Collection.class.isInstance(pXmls)) {
final File webapp = webModule.getFile();
if (webapp == null) {
continue;
}
final String webappAbsolutePath = webapp.getAbsolutePath();
final Collection<URL> list = Collection.class.cast(pXmls);
for (final URL url : list) {
try {
final File file = URLs.toFile(url);
if (file.getAbsolutePath().startsWith(webappAbsolutePath)) {
foundRootUrls.add(url);
}
} catch (final IllegalArgumentException iae) {
// no-op
}
}
}
webModule.getAltDDs().put(EAR_WEBAPP_PERSISTENCE_XML_JARS, foundRootUrls);
}
for (final DeploymentModule module : appModule.getDeploymentModule()) {
module.setStandaloneModule(false);
}
return appModule;
} catch (final OpenEJBException e) {
logger.error("Unable to load EAR: " + jarPath, e);
throw e;
}
}
private void createApplicationFromFiles(final String appId, final ClassLoader tmpClassLoader, final Map<String, URL> ejbModules, final Map<String, URL> clientModules, final Map<String, URL> resouceModules, final Map<String, URL> webModules, final HashMap<String, URL> files) throws OpenEJBException {
for (final Map.Entry<String, URL> entry : files.entrySet()) {
// if (entry.getKey().startsWith("lib/")) continue;// will not be scanned since we don't get folder anymore
if (!entry.getKey().matches(".*\\.(jar|war|rar|ear)")) {
continue;
}
try {
detectAndAddModuleToApplication(appId, tmpClassLoader, ejbModules, clientModules, resouceModules, webModules, entry);
} catch (final UnsupportedOperationException | UnknownModuleTypeException e) {
// Ignore it as per the javaee spec EE.8.4.2 section 1.d.iii
logger.info("Ignoring unknown module type: " + entry.getKey());
} catch (final Exception e) {
throw new OpenEJBException("Unable to determine the module type of " + entry.getKey() + ": Exception: " + e.getMessage(), e);
}
}
}
private void detectAndAddModuleToApplication(final String appId, final ClassLoader tmpClassLoader, final Map<String, URL> ejbModules, final Map<String, URL> clientModules, final Map<String, URL> resouceModules, final Map<String, URL> webModules, final Map.Entry<String, URL> entry) throws IOException, UnknownModuleTypeException {
final ClassLoader moduleClassLoader = ClassLoaderUtil.createTempClassLoader(appId, new URL[]{entry.getValue()}, tmpClassLoader);
final Class<? extends DeploymentModule> moduleType = discoverModuleType(entry.getValue(), moduleClassLoader, true);
if (EjbModule.class.equals(moduleType)) {
ejbModules.put(entry.getKey(), entry.getValue());
} else if (ClientModule.class.equals(moduleType)) {
clientModules.put(entry.getKey(), entry.getValue());
} else if (ConnectorModule.class.equals(moduleType)) {
resouceModules.put(entry.getKey(), entry.getValue());
} else if (WebModule.class.equals(moduleType)) {
webModules.put(entry.getKey(), entry.getValue());
}
}
protected ClientModule createClientModule(final URL clientUrl, final String absolutePath, final ClassLoader appClassLoader, final String moduleName) throws OpenEJBException {
return createClientModule(clientUrl, absolutePath, appClassLoader, moduleName, true);
}
protected ClientModule createClientModule(final URL clientUrl, final String absolutePath, final ClassLoader appClassLoader, final String moduleName, final boolean log) throws OpenEJBException {
final ResourceFinder clientFinder = new ResourceFinder(clientUrl);
URL manifestUrl = null;
try {
manifestUrl = clientFinder.find("META-INF/MANIFEST.MF");
} catch (final IOException e) {
//
}
String mainClass = null;
if (manifestUrl != null) {
try {
final InputStream is = IO.read(manifestUrl);
final Manifest manifest = new Manifest(is);
mainClass = manifest.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
} catch (final IOException e) {
throw new OpenEJBException("Unable to determine Main-Class defined in META-INF/MANIFEST.MF file", e);
}
}
// if (mainClass == null) throw new IllegalStateException("No Main-Class defined in META-INF/MANIFEST.MF file");
final Map<String, URL> descriptors = getDescriptors(clientFinder, log);
ApplicationClient applicationClient = null;
final URL clientXmlUrl = descriptors.get("application-client.xml");
if (clientXmlUrl != null) {
applicationClient = ReadDescriptors.readApplicationClient(clientXmlUrl);
}
final ClientModule clientModule = new ClientModule(applicationClient, appClassLoader, absolutePath, mainClass, moduleName);
clientModule.getAltDDs().putAll(descriptors);
if (absolutePath != null) {
clientModule.getWatchedResources().add(absolutePath);
}
if (clientXmlUrl != null && "file".equals(clientXmlUrl.getProtocol())) {
clientModule.getWatchedResources().add(URLs.toFilePath(clientXmlUrl));
}
return clientModule;
}
protected EjbModule createEjbModule(final URL baseUrl, final String jarPath, final ClassLoader classLoader) throws OpenEJBException {
// read the ejb-jar.xml file
Map<String, URL> descriptors;
if (baseUrl != null) {
descriptors = getDescriptors(baseUrl);
} else {
try {
descriptors = getDescriptors(classLoader, null);
} catch (final IOException e) {
descriptors = new HashMap<>();
}
}
EjbJar ejbJar = null;
final URL ejbJarXmlUrl = descriptors.get("ejb-jar.xml");
if (ejbJarXmlUrl != null) {
try {
ejbJar = ReadDescriptors.readEjbJar(ejbJarXmlUrl.openStream());
} catch (final IOException e) {
throw new OpenEJBException(e);
}
}
// create the EJB Module
final EjbModule ejbModule = new EjbModule(classLoader, null, jarPath, ejbJar, null);
ejbModule.getAltDDs().putAll(descriptors);
if (jarPath != null) {
ejbModule.getWatchedResources().add(jarPath);
}
if (ejbJarXmlUrl != null && "file".equals(ejbJarXmlUrl.getProtocol())) {
ejbModule.getWatchedResources().add(URLs.toFilePath(ejbJarXmlUrl));
}
ejbModule.setClientModule(createClientModule(baseUrl, jarPath, classLoader, null, false));
// load webservices descriptor
addWebservices(ejbModule);
return ejbModule;
}
private WebModule createWebModule(final String jar, final URL warUrl, final ClassLoader parentClassLoader, final String contextRoot, final String moduleName, final ExternalConfiguration config) throws OpenEJBException {
return createWebModule(jar, URLs.toFilePath(warUrl), parentClassLoader, contextRoot, moduleName, config);
}
public void addWebModule(final AppModule appModule, final URL warUrl, final ClassLoader parentClassLoader, final String contextRoot, final String moduleName) throws OpenEJBException {
final WebModule webModule = createWebModule(appModule.getJarLocation(), URLs.toFilePath(warUrl), parentClassLoader, contextRoot, moduleName, null);
addWebModule(webModule, appModule);
}
public static EjbModule addWebModule(final WebModule webModule, final AppModule appModule) throws OpenEJBException {
// create and add the WebModule
appModule.getWebModules().add(webModule);
if (appModule.isStandaloneModule()) {
appModule.getAdditionalLibraries().addAll(webModule.getUrls());
}
{
final Object pXml = appModule.getAltDDs().get("persistence.xml");
List<URL> persistenceXmls = pXml == null ? null : (List.class.isInstance(pXml) ? (List<URL>) pXml : new ArrayList<>(asList(URL.class.cast(pXml))));
if (persistenceXmls == null) {
persistenceXmls = new ArrayList<>();
appModule.getAltDDs().put("persistence.xml", persistenceXmls);
}
final Object o = webModule.getAltDDs().get("persistence.xml");
if (o instanceof URL) {
final URL url = (URL) o;
persistenceXmls.add(url);
}
if (o instanceof List) {
final List<URL> urls = (List<URL>) o;
persistenceXmls.addAll(urls);
}
}
// Per the Spec version of the Collapsed EAR there
// aren't individual EjbModules inside a war.
// The war itself is one big EjbModule if certain
// conditions are met. These conditions are different
// than an ear file, so the ear-style code we were previously
// using doesn't exactly work anymore.
final EjbModule webEjbModule = new EjbModule(webModule.getClassLoader(), webModule.getModuleId(), webModule.getJarLocation(), null, null);
webEjbModule.setWebapp(true);
webEjbModule.getAltDDs().putAll(webModule.getAltDDs());
appModule.getEjbModules().add(webEjbModule);
try {
// TODO: Put our scanning ehnancements back, here
fillEjbJar(webModule, webEjbModule);
if (webModule.getFinder() == null) {
if (isMetadataComplete(webModule, webEjbModule)) {
final IAnnotationFinder finder = new org.apache.xbean.finder.AnnotationFinder(new ClassesArchive());
webModule.setFinder(finder);
webEjbModule.setFinder(finder);
} else {
final IAnnotationFinder finder = FinderFactory.createFinder(webModule);
webModule.setFinder(finder);
webEjbModule.setFinder(finder);
}
} else if (webEjbModule.getFinder() == null) {
webEjbModule.setFinder(webModule.getFinder());
}
} catch (final Exception e) {
throw new OpenEJBException("Unable to create annotation scanner for web module " + webModule.getModuleId(), e);
}
addWebservices(webEjbModule);
return webEjbModule;
}
/**
* If the web.xml is metadata-complete and there is no ejb-jar.xml
* then per specification we use the web.xml metadata-complete setting
* to imply the same for EJBs.
*
* @param webModule WebModule
* @param ejbModule EjbModule
*/
private static void fillEjbJar(final WebModule webModule, final EjbModule ejbModule) {
final Object o = webModule.getAltDDs().get("ejb-jar.xml");
if (o != null) {
return;
}
if (ejbModule.getEjbJar() != null) {
return;
}
final EjbJar ejbJar = new EjbJar();
final WebApp webApp = webModule.getWebApp();
ejbJar.setMetadataComplete(webApp.isMetadataComplete());
ejbModule.setEjbJar(ejbJar);
}
private static boolean isMetadataComplete(final WebModule webModule, final EjbModule ejbModule) {
if (webModule.getWebApp() == null) {
return false;
}
if (!webModule.getWebApp().isMetadataComplete()) {
return false;
}
// At this point we know the web.xml is metadata-complete
// We need to determine if there are cdi or ejb xml files
if (webModule.getAltDDs().get("beans.xml") == null) {
return true;
}
if (ejbModule.getEjbJar() == null) {
return true;
}
return ejbModule.getEjbJar().isMetadataComplete();
}
public WebModule createWebModule(final String appId, final String warPath, final ClassLoader parentClassLoader, final String contextRoot, final String moduleName, final ExternalConfiguration config) throws OpenEJBException {
File warFile = new File(warPath);
if (!warFile.isDirectory()) {
warFile = unpack(warFile);
}
// read web.xml file
final Map<String, URL> descriptors;
try {
descriptors = getWebDescriptors(warFile);
} catch (final IOException e) {
throw new OpenEJBException("Unable to collect descriptors in web module: " + contextRoot, e);
}
final WebApp webApp;
final URL webXmlUrl = descriptors.get("web.xml");
if (webXmlUrl != null) {
webApp = ReadDescriptors.readWebApp(webXmlUrl);
} else {
// no web.xml webapp - possible since Servlet 3.0
webApp = new WebApp();
}
// determine war class path
final List<URL> webUrls = new ArrayList<>();
ensureContainerUrls();
webUrls.addAll(containerUrls);
final SystemInstance systemInstance = SystemInstance.get();
// add these urls first to ensure we load classes from here first
final String externalRepos = systemInstance.getProperty("tomee." + warFile.getName().replace(".war", "") + ".externalRepositories");
List<URL> externalUrls = null;
if (externalRepos != null) {
externalUrls = new ArrayList<URL>();
for (final String additional : externalRepos.split(",")) {
final String trim = additional.trim();
if (!trim.isEmpty()) {
try {
externalUrls.add(new File(trim).toURI().toURL());
} catch (final MalformedURLException e) {
logger.error(e.getMessage());
}
}
}
webUrls.addAll(externalUrls);
}
final Map<String, URL[]> urls = getWebappUrlsAndRars(warFile);
webUrls.addAll(Arrays.asList(urls.get(URLS_KEY)));
final List<URL> addedUrls = new ArrayList<URL>();
for (final URL url : urls.get(RAR_URLS_KEY)) { // eager unpack to be able to use it in classloader
final File[] files = unpack(URLs.toFile(url)).listFiles();
if (files != null) {
for (final File f : files) {
if (f.getName().endsWith(".jar")) {
try {
addedUrls.add(f.toURI().toURL());
} catch (final MalformedURLException e) {
logger.warning("War path bad: " + f.getAbsolutePath(), e);