Skip to content

Package: WAR

WAR

nameinstructionbranchcomplexitylinemethod
WAR(DeployedService)
M: 59 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
compileJavac()
M: 68 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
compileWSDL(WsTool)
M: 239 C: 0
0%
M: 24 C: 0
0%
M: 13 C: 0
0%
M: 41 C: 0
0%
M: 1 C: 0
0%
copyClasspath(Realm)
M: 58 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
copyResources(File)
M: 8 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
copyToClasses(File[])
M: 26 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
copyToWEBINF(File)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
copyWsit(File)
M: 15 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
generateSunJaxWsXml(List)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
generateSunWebXml()
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
generateWSDL(WsTool)
M: 176 C: 0
0%
M: 16 C: 0
0%
M: 9 C: 0
0%
M: 31 C: 0
0%
M: 1 C: 0
0%
generateWebXml(List, boolean)
M: 40 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
getEndpointsInfos()
M: 388 C: 0
0%
M: 40 C: 0
0%
M: 21 C: 0
0%
M: 79 C: 0
0%
M: 1 C: 0
0%
getWSDL()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
zipTo(File)
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: package com.sun.xml.ws.test.container;
12:
13: import com.sun.istack.NotNull;
14: import com.sun.xml.ws.test.CodeGenerator;
15: import com.sun.xml.ws.test.Realm;
16: import com.sun.xml.ws.test.World;
17: import com.sun.xml.ws.test.container.jelly.EndpointInfoBean;
18: import com.sun.xml.ws.test.container.jelly.WebXmlInfoBean;
19: import com.sun.xml.ws.test.model.TestEndpoint;
20: import com.sun.xml.ws.test.model.WSDL;
21: import com.sun.xml.ws.test.tool.WsTool;
22: import com.sun.xml.ws.test.util.ArgumentListBuilder;
23: import com.sun.xml.ws.test.util.FileUtil;
24: import com.sun.xml.ws.test.util.FreeMarkerTemplate;
25: import com.sun.xml.ws.test.util.JavacTask;
26: import org.apache.tools.ant.taskdefs.Jar;
27: import org.apache.tools.ant.taskdefs.Zip;
28: import org.apache.tools.ant.types.Path;
29:
30: import jakarta.jws.WebService;
31: import javax.xml.namespace.QName;
32: import jakarta.xml.ws.WebEndpoint;
33: import jakarta.xml.ws.WebServiceClient;
34: import jakarta.xml.ws.WebServiceProvider;
35: import java.io.File;
36: import java.lang.reflect.Method;
37: import java.net.URL;
38: import java.net.URLClassLoader;
39: import java.util.ArrayList;
40: import java.util.HashMap;
41: import java.util.List;
42: import java.util.Map.Entry;
43: import java.util.Set;
44: import javax.xml.parsers.DocumentBuilderFactory;
45: import org.w3c.dom.Document;
46:
47: /**
48: * Represents an exploded WAR file on a file system.
49: *
50: * This class primarily contains operations that generate various files that
51: * constitute a war file.
52: */
53: public final class WAR {
54: /**
55: * The root directory of the war image.
56: */
57: public final File root;
58:
59: /**
60: * "build/classes" directory under the work directory. It is used
61: * often enough that it is created here to avoid typo errors.
62: */
63: public final File classDir;
64:
65: /**
66: * "WEB-INF" directory under the work directory. It is used
67: * often enough that it is created here to avoid typo errors.
68: */
69: public final File webInfDir;
70:
71: /**
72: * "WEB-INF/lib" directory.
73: */
74: public final File libDir;
75:
76: /**
77: * Directory to put additional generated source files.
78: */
79: public final File srcDir;
80:
81: /**
82: * One web application may end up having multiple WSDLs if a fromjava service
83: * contains multiple @WebService classes.
84: */
85: private final List<File> wsdl = new ArrayList<File>();
86:
87: /**
88: * This war file is created for this service.
89: */
90: public final DeployedService service;
91:
92:
93: public WAR(DeployedService service) {
94: this.service = service;
95: root = service.warDir;
96: webInfDir = new File(root,"WEB-INF");
97: classDir = new File(webInfDir,"classes");
98: classDir.mkdirs();
99: libDir = new File(webInfDir,"lib");
100: libDir.mkdir();
101: srcDir = new File(service.workDir,"gen-src");
102: srcDir.mkdir();
103: }
104:
105: /**
106: * This method collects info about endpoints; it's being used for generating
107: * server side descriptors.
108: *
109: * @return
110: * @throws Exception
111: */
112: public List<EndpointInfoBean> getEndpointsInfos() throws Exception {
113:
114: Set<TestEndpoint> endpoints = service.service.endpoints;
115: ArrayList<EndpointInfoBean> beans = new ArrayList<EndpointInfoBean>();
116:
117: // fromJava:
118:• if (service.service.wsdl.isEmpty() || service.service.parent.metadatafiles.size() > 0) {
119:
120:• for (TestEndpoint endpoint : endpoints) {
121: EndpointInfoBean bean = EndpointInfoBean.create(
122: endpoint.name,
123: endpoint.className,
124: null,
125: null,
126: null,
127: null,
128: "/" + endpoint.name);
129: beans.add(bean);
130: }
131: } else { // fromWSDL:
132:
133: ArrayList<String> portNames = new ArrayList<String>();
134:
135: // if we find multiple implClass, use the port local name to match them
136: HashMap<String, String> portNameToImpl = new HashMap<String, String>();
137: HashMap<String, String> implToPort = new HashMap<String, String>();
138: // port name to service name
139: HashMap<String, QName> portNameToServiceName = new HashMap<String, QName>();
140:
141: String implClass = null;
142:
143: ClassLoader loader = new URLClassLoader(
144: new URL[]{classDir.toURL()},
145: World.runtime.getClassLoader()
146: );
147:
148: WebServiceClient wsca = null;
149:• for (String className : FileUtil.getClassFileNames(classDir)) {
150:
151: Class clazz = loader.loadClass(className);
152:
153: // prevent setting null ...
154:• if (clazz.getAnnotation(WebServiceClient.class) != null) {
155: wsca = (WebServiceClient) clazz.getAnnotation(WebServiceClient.class);
156:• for (Method method : clazz.getMethods()) {
157: WebEndpoint a = method.getAnnotation(WebEndpoint.class);
158:• if (a != null && method.getParameterTypes().length == 0) {
159: String name = a.name();
160:• if (!name.equals("")) {
161: portNames.add(name);
162: portNameToServiceName.put(name, new QName(wsca.targetNamespace(), wsca.name()));
163: }
164: }
165: }
166: continue;
167: }
168:
169: WebService ws = (WebService) clazz.getAnnotation(WebService.class);
170:• if (ws != null) {
171: String endpointInterface = ws.endpointInterface();
172:• if (!endpointInterface.equals(""))
173: implClass = clazz.getName();
174:• if (!"".equals(ws.portName())) {
175: portNameToImpl.put(ws.portName(), implClass);
176: implToPort.put(implClass, ws.portName());
177: }
178: } else {
179: WebServiceProvider wsp = (WebServiceProvider) clazz.getAnnotation(WebServiceProvider.class);
180:• if (wsp != null) {
181: implClass = clazz.getName();
182:• if (!"".equals(wsp.portName())) {
183: portNameToImpl.put(wsp.portName(), implClass);
184: implToPort.put(implClass, wsp.portName());
185: }
186: }
187: }
188: }
189:
190: // create endpoint info beans
191: String tns = null;
192: String wsdlLocation = null;
193:
194:• if (wsca != null) {
195: tns = wsca.targetNamespace();
196:
197: // hacky
198: wsdlLocation = wsca.wsdlLocation();
199: wsdlLocation = wsdlLocation.replace('\\', '/');
200: wsdlLocation = "WEB-INF/wsdl/" +
201: wsdlLocation.substring(
202: wsdlLocation.lastIndexOf("/") + 1,
203: wsdlLocation.length());
204: }
205:
206: int i = 0;
207:• for (String portName : portNames) {
208: String impl = portNameToImpl.get(portName);
209: portNameToImpl.remove(portName);
210:• if (impl == null) {
211:• if (portNames.size() > 1) {
212: continue; // OK to have a portname without a deployed endpoint
213: }
214: impl = implClass; // default
215: }
216:
217: // call EndpointInfoBean.create thanks to static import.
218: // this allows us to create an instance of class that's not visible to this classloader.
219: EndpointInfoBean bean = EndpointInfoBean.create(
220: "endpoint" + (i++),
221: impl,
222: wsdlLocation,
223: portNameToServiceName.get(portName),
224: new QName(tns, portName),
225: "binding",
226: "/" + service.service.getEndpointByImpl(impl).name);
227: beans.add(bean);
228: implToPort.remove(impl);
229: }
230:
231:• for (Entry<String, String> e : implToPort.entrySet()) {
232: EndpointInfoBean bean = EndpointInfoBean.create(
233: "endpoint" + (i++),
234: e.getKey(),
235: wsdlLocation,
236: portNameToServiceName.get(e.getValue()),
237: new QName(tns, e.getValue()),
238: "binding",
239: "/" + service.service.getEndpointByImpl(e.getKey()).name);
240: beans.add(bean);
241: }
242: // error check
243:• if (!portNameToImpl.isEmpty())
244: throw new Exception("Implementations " + new ArrayList(portNameToImpl.values()) + " don't have corresponding ports in WSDL." +
245: " Their declared ports are " + new ArrayList(portNameToImpl.keySet()) +
246: " but actual ports are " + portNames
247: );
248: }
249: return beans;
250: }
251:
252: /**
253: * Creates a war archive from the exploded image at {@link #root}.
254: */
255: public void zipTo(File archive) throws Exception {
256: Zip zip = new Zip();
257: zip.setProject(World.project);
258: zip.setDestFile(archive);
259: zip.setBasedir(root);
260: zip.execute();
261: }
262:
263: /**
264: * Copies the classpath specified by the given {@link Path}
265: * into <code>WEB-INF/lib</code> and <code>WEB-INF/classes</code>
266: */
267: public void copyClasspath(Realm classpath) throws Exception {
268: int n = 0;
269:• for (File path : classpath.list()) {
270:• if(path.isFile())
271: // just copy one jar
272: FileUtil.copyFile(path,new File(libDir,path.getName()));
273: else {
274: // create an uncompressed jar file. This serves a few purposes.
275: // - in general file systems are not good at dealing with many small files
276: // - we'll do the archiving anyway when we pack this into a jar
277: Jar jar = new Jar();
278: jar.setProject(World.project);
279: jar.setDestFile(new File(libDir,"generated"+(n++)+".jar"));
280: jar.setBasedir(path);
281: jar.setCompress(false);
282: jar.execute();
283: }
284: }
285: }
286:
287: /**
288: * Copies handler files in to WEB-INF/classes
289: */
290: public void copyToClasses(File ... handlerConfigs) {
291:• for (File config : handlerConfigs) {
292: FileUtil.copyFile(config, new File(classDir, config.getName()));
293: }
294: }
295:
296: /**
297: * Copies web.xml to WEB-INF/
298: */
299: public void copyToWEBINF(File file) {
300: FileUtil.copyFile(file, new File(webInfDir, file.getName()));
301: }
302:
303: /**
304: * Copies resources under the directory in to <code>WEB-INF/classes</code>
305: */
306: public void copyResources(File resourcesDir) {
307:• if(resourcesDir != null) {
308: FileUtil.copyDir(resourcesDir, classDir, "**/wsit-client.xml");
309: }
310: }
311:
312: public void copyWsit(File wsitConf) {
313:• if(wsitConf != null && wsitConf.exists()) {
314: FileUtil.copyFile(wsitConf, new File(classDir, wsitConf.getName()));
315: }
316: }
317:
318: /**
319: * Gets the path of the WSDL.
320: *
321: * <p>
322: * This is either copied from the test data (for "fromwsdl" tests),
323: * or generated (for "fromjava" tests.) For fromjava tests with
324: * multiple <code>WebService</code> classes, you may get more than one WSDLs.
325: *
326: * <p>
327: * In a situation where there's no WSDL and just provider service,
328: * the list may be empty.
329: */
330: public @NotNull List<File> getWSDL() {
331: return wsdl;
332: }
333:
334: /**
335: * This method uses Jelly to write the sun-jaxws.xml file. The
336: * template file is sun-jaxws.jelly.
337: *
338: * @return
339: * list of endpoints that were discovered.
340: */
341: final void generateSunJaxWsXml(List<EndpointInfoBean> endpointInfoBeans) throws Exception {
342: FreeMarkerTemplate jelly = new FreeMarkerTemplate("/web/sun-jaxws.ftl");
343: jelly.put("endpointInfoBeans", endpointInfoBeans);
344: jelly.run(new File(webInfDir, "sun-jaxws.xml"));
345: }
346:
347: /**
348: * This method uses Jelly to write the web.xml file. The
349: * template file is web.jelly. The real work happens
350: * in the WebXmlInfoBean object which supplies information
351: * to the Jelly processor through accessor methods.
352: *
353: * @see WebXmlInfoBean
354: */
355: final void generateWebXml(List<EndpointInfoBean> endpoints, boolean httpspi) throws Exception {
356: FreeMarkerTemplate jelly = new FreeMarkerTemplate("/web/web.ftl");
357:• String listenerClass = httpspi
358: ? "com.sun.xml.ws.transport.httpspi.servlet.WSSPIContextListener"
359: : "com.sun.xml.ws.transport.http.servlet.WSServletContextListener" ;
360:• String servletClass = httpspi
361: ? "com.sun.xml.ws.transport.httpspi.servlet.WSSPIServlet"
362: : "com.sun.xml.ws.transport.http.servlet.WSServlet";
363: WebXmlInfoBean infoBean = new WebXmlInfoBean(service.parent,endpoints, listenerClass, servletClass );
364: jelly.put("data", infoBean);
365: jelly.run(new File(webInfDir, "web.xml"));
366: }
367:
368: /**
369: * Generates <code>sun-web.xml</code>
370: */
371: final void generateSunWebXml() throws Exception {
372: FreeMarkerTemplate jelly = new FreeMarkerTemplate("/web/sun-web.ftl");
373: jelly.run(new File(webInfDir, "sun-web.xml"));
374: }
375:
376: /**
377: * Generate server artifacts from WSDL.
378: */
379: final void compileWSDL(WsTool wsimport) throws Exception {
380:• assert !service.service.wsdl.isEmpty();
381:• assert this.wsdl.isEmpty();
382:
383:• for (WSDL dl : service.service.wsdl) {
384: ArgumentListBuilder options = new ArgumentListBuilder();
385: //Add customization files
386:• for (File custFile : service.service.customizations) {
387: options.add("-b").add(custFile);
388: }
389: options.add("-extension");
390: //Don't add the default package option if -noPackage is specified
391: // this will be helpful in testing default/customization behavior.
392:• if (!service.service.parent.testOptions.contains("-noPackage")) {
393:
394: // set package name if not specified in wsimport-server options
395:• if (!service.service.parent.wsimportServerOptions.contains("-p")) {
396: options.add("-p").add(service.service.getGlobalUniqueName());
397: }
398: }
399: //Other options
400:• if (World.debug) {
401: options.add("-verbose");
402: }
403: options.add("-s").add(srcDir);
404: options.add("-d").add(classDir);
405: options.add("-Xnocompile");
406:• if (Boolean.getBoolean("harness.useSSL")) {
407: options.add("-XdisableSSLHostnameVerification");
408: }
409: options.add(dl.wsdlFile);
410: options.addAll(service.service.parent.wsimportServerOptions);
411:• if (!wsimport.isNoop()) {
412: System.out.println("Generating server artifacts from " + dl.wsdlFile);
413: options.invoke(wsimport);
414: }
415:
416: // copy WSDL into a war file
417: File wsdlDir = new File(webInfDir, "wsdl");
418:• if (dl.relativeLocation != null) {
419: wsdlDir = new File(wsdlDir, dl.relativeLocation);
420: }
421: wsdlDir.mkdirs();
422:
423: File src = dl.wsdlFile;
424: File wsdlFile = new File(wsdlDir, src.getName());
425: this.wsdl.add(wsdlFile);
426:
427: FileUtil.copyFile(src, wsdlFile);
428:• for (File importedWsdl : dl.importedWsdls) {
429: String importedPath = importedWsdl.getCanonicalPath().substring(src.getParentFile().getCanonicalPath().length() + 1);
430: FileUtil.copyFile(importedWsdl, new File(wsdlDir, importedPath));
431: }
432:• for (File schema : dl.schemas) {
433: String importedPath = schema.getCanonicalPath().substring(src.getParentFile().getCanonicalPath().length() + 1);
434: FileUtil.copyFile(schema, new File(wsdlDir, importedPath));
435: }
436: }
437: }
438:
439: /**
440: * Compiles Java source files into <code>WEB-INF/classes</code>.
441: */
442: final void compileJavac() throws Exception {
443: JavacTask javac = new JavacTask(service.parent.descriptor.javacOptions);
444:• if(service.parent.descriptor.common != null)
445: javac.setSourceDir(service.service.baseDir, srcDir,
446: service.parent.descriptor.common );
447: else
448: javac.setSourceDir(service.service.baseDir, srcDir);
449:
450: javac.setDestdir(classDir);
451: javac.setDebug(true);
452: CodeGenerator.generateJavac(javac);
453: javac.execute();
454: }
455:
456: /**
457: * Generates a WSDL into a war file if this is "fromjava" service.
458: */
459: final void generateWSDL(WsTool wsgen) throws Exception {
460:• assert service.service.wsdl.isEmpty();
461:
462: // Use wsgen to generate the artifacts
463: File wsdlDir = new File(webInfDir, "wsdl");
464: wsdlDir.mkdirs();
465:
466:• for (TestEndpoint endpt : service.service.endpoints) {
467:• if(endpt.isProvider) continue;
468: ArgumentListBuilder options = new ArgumentListBuilder();
469: options.add("-wsdl");
470:• if(World.debug)
471: options.add("-verbose");
472: options.add("-r").add(wsdlDir);
473: Path cp = new Path(World.project);
474: cp.createPathElement().setLocation(classDir);
475: cp.add(World.tool.getPath());
476: cp.add(World.runtime.getPath());
477:• if(World.debug)
478: System.out.println("wsgen classpath arg = " + cp);
479: options.add("-cp").add(cp);
480:• options.add("-s").add(service.service.parent.disgardWsGenOutput ? NOWHERE : classDir);
481:• options.add("-d").add(service.service.parent.disgardWsGenOutput ? NOWHERE : classDir);
482:
483: // obtain a report file from wsgen
484: File report = new File(wsdlDir,"wsgen.report");
485: options.add("-XwsgenReport").add(report);
486:
487: // additional wsgen options from test descriptor
488: options.addAll(service.service.parent.wsgenOptions);
489:
490: options.add(endpt.className);
491:
492: System.out.println("Generating WSDL");
493:• if(World.debug)
494: System.out.println(options);
495: options.invoke(wsgen);
496:
497: // parse report
498: Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(report);
499: wsdl.add(new File(dom.getDocumentElement().getElementsByTagName("wsdl").item(0).getTextContent().trim()));
500: }
501: }
502:
503: private static final File NOWHERE = new File(System.getProperty("java.io.tmpdir"));
504: }