Skip to content

Package: TestService$HandlersFilter

TestService$HandlersFilter

nameinstructionbranchcomplexitylinemethod
TestService.HandlersFilter(TestService)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
accept(File, String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2018 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.model;
12:
13: import com.sun.istack.NotNull;
14: import com.sun.istack.Nullable;
15:
16: import java.io.File;
17: import java.io.FileFilter;
18: import java.io.FilenameFilter;
19: import java.io.IOException;
20: import java.lang.reflect.Constructor;
21: import java.util.ArrayList;
22: import java.util.Arrays;
23: import java.util.Collections;
24: import java.util.Comparator;
25: import java.util.LinkedHashSet;
26: import java.util.List;
27: import java.util.Set;
28: import javax.tools.DiagnosticCollector;
29: import javax.tools.JavaCompiler;
30: import javax.tools.JavaFileObject;
31: import javax.tools.StandardJavaFileManager;
32: import javax.tools.ToolProvider;
33:
34: /**
35: * A service to be deployed for a test.
36: *
37: * <p>
38: * TODO: We need to be able to mark service as an EJB service
39: *
40: * @author Kohsuke Kawaguchi
41: */
42: public class TestService {
43:
44: /**
45: * Name of the service.
46: *
47: * The name must be:
48: * <ol>
49: * <li>Unique within {@link TestDescriptor}
50: * <li>an empty string or a valid Java identifier.
51: * </ol>
52: *
53: * <p>
54: * An empty string is convenient to describe the default/primary service
55: * (or when there's just one service involved, which is the majority.)
56: */
57: @NotNull
58: public final String name;
59:
60: /**
61: * Directory in which the service's source files reside.
62: */
63: @NotNull
64: public final File baseDir;
65:
66: /**
67: * Optional WSDL file that describes this service.
68: */
69: @Nullable
70: public final List<WSDL> wsdl;
71:
72: /**
73: * Possibly empty list of JAXB/JAX-WS external binding customizations.
74: *
75: * Must be empty when {@link #wsdl} is null.
76: */
77: @NotNull
78: public final List<File> customizations = new ArrayList<File>();
79:
80: /**
81: * {@link TestEndpoint}s that this service exposes.
82: *
83: * <p>
84: * The harness uses this information to inject proxies to the client.
85: */
86: @NotNull
87: public final Set<TestEndpoint> endpoints = new LinkedHashSet<TestEndpoint>();
88:
89: public final TestDescriptor parent;
90:
91: /**
92: * Determines if the service is an STS for WSTrust and needs special handling
93: */
94: public boolean isSTS;
95:
96: /**
97: * @param explicitServiceClassName
98: * Descriptor can explicitly specify the service class name.
99: * If this happens, we won't search for @WebService classes and just use this instead.
100: * Used for deploying inner classes and testing inheritance.
101: */
102: public TestService(TestDescriptor parent, String name, File baseDir, List<WSDL> wsdl, boolean sts, @Nullable String explicitServiceClassName) throws IOException {
103: this.parent = parent;
104: this.name = name;
105: this.wsdl = wsdl;
106: this.baseDir = baseDir;
107: this.isSTS = sts;
108:
109: if(explicitServiceClassName==null) {
110: // search for classes with @WebService
111: findEndpoints(baseDir);
112: } else {
113: String shortName = explicitServiceClassName.substring(explicitServiceClassName.lastIndexOf('.')+1);
114: endpoints.add(new TestEndpoint(shortName,explicitServiceClassName,null,false));
115: }
116: }
117:
118: public String getAbsolutePath(String relativePath) {
119: return baseDir.getAbsolutePath() + File.separator + relativePath;
120: }
121:
122: /**
123: * Gets the {@link TestEndpoint} that has the specified implementation class.
124: */
125: public TestEndpoint getEndpointByImpl(String implClassFullName) {
126: for (TestEndpoint ep : endpoints) {
127: if(ep.className.equals(implClassFullName))
128: return ep;
129: }
130: throw new Error("No TestEndpoint object recorded for "+implClassFullName);
131: }
132:
133: /**
134: * Returns the name combined with the test case name to make a globaly unique name
135: * for this service.
136: */
137: public String getGlobalUniqueName() {
138: if(name.length()==0)
139: return parent.name;
140: else
141: return parent.name +'.'+name;
142: }
143:
144: /**
145: * Scans the Java source code in the server directory and
146: * find all classes with @WebService. Those are turned into
147: * {@link TestEndpoint}.
148: */
149: private void findEndpoints(File dir) throws IOException {
150: File[] dirs = dir.listFiles(new FileFilter() {
151: public boolean accept(File child) {
152: // don't go in our own work directory
153: return child.isDirectory() && !child.getName().equals("work");
154: }
155: });
156: Arrays.sort(dirs, new Comparator<File>() {
157:
158: public int compare(File f1, File f2) {
159: return f2.compareTo(f1);
160: }
161: });
162: for (File subdir : dirs) {
163: findEndpoints(subdir);
164: }
165:
166: File[] javas = dir.listFiles(new FileFilter() {
167: public boolean accept(File child) {
168: return child.getName().endsWith(".java");
169: }
170: });
171:
172: if (javas.length > 0) {
173: // parse the Java file, looking for @WebService/Provider
174: // (note that at this point those source files by themselves won't compile)
175: JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
176:
177: // TODO: workaround for Jake/Jigsaw. Remove after fixing https://bugs.openjdk.java.net/browse/JDK-6929461
178: if (compiler == null) {
179: try {
180: Class compilerClass = Class.forName("com.sun.tools.javac.api.JavacTool");
181: Constructor constructor = compilerClass.getConstructor();
182: compiler = (JavaCompiler) constructor.newInstance();
183: } catch (Throwable ignored) {
184: ignored.printStackTrace();
185: }
186: }
187: // </TODO>
188:
189: DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
190: StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
191: JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager,
192: diagnostics, new ArrayList<String>() {{add("-proc:only");}}, null,
193: fileManager.getJavaFileObjects(javas));
194: EndpointReader r = new EndpointReader();
195: task.setProcessors(Collections.singleton(r));
196: task.call();
197: endpoints.addAll(r.getTestEndpoints());
198: }
199: }
200:
201: @Override
202: public String toString() {
203: return name+" of "+parent.toString();
204: }
205:
206: /**
207: * This filter gives all handler configuration files in the directory.
208: * i.e files matching pattern *handlers.xml
209: */
210: class HandlersFilter implements FilenameFilter {
211: public boolean accept(File dir, String name) {
212: return (name.endsWith("handlers.xml"));
213: }
214: }
215:
216: public File[] getHandlerConfiguration() {
217: return baseDir.listFiles(new HandlersFilter());
218: }
219:
220: /**
221: * This filter gives all web.xml files in the directory.
222: * i.e files with name web.xml
223: */
224: class NameFilter implements FilenameFilter {
225: String filename;
226: NameFilter(String filename) {
227: this.filename = filename;
228: }
229: public boolean accept(File dir, String name) {
230: return (name.equals(filename));
231: }
232: }
233:
234: public File getConfiguredFile(String filename) {
235: return first(baseDir.listFiles(new NameFilter(filename)));
236: }
237:
238: private File first(File[] files) {
239: return files == null || files.length == 0 ? null: files[0];
240: }
241: }