Skip to content

Package: HelidonResourceLoader

HelidonResourceLoader

nameinstructionbranchcomplexitylinemethod
HelidonResourceLoader(String, boolean)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getCatalogFile()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getResource(String)
M: 15 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getResourcePaths(String)
M: 64 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%
loadResources(String, URL, Set)
M: 79 C: 0
0%
M: 7 C: 0
0%
M: 5 C: 0
0%
M: 22 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 5 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) 2021, 2024 Oracle and/or its affiliates.
3: *
4: * Licensed under the Apache License, Version 2.0 (the "License");
5: * you may not use this file except in compliance with the License.
6: * You may obtain a copy of the License at
7: *
8: * http://www.apache.org/licenses/LICENSE-2.0
9: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.eclipse.metro.helidon;
17:
18: import com.sun.xml.ws.transport.http.DeploymentDescriptorParser;
19: import com.sun.xml.ws.transport.http.ResourceLoader;
20: import java.io.IOException;
21: import java.net.MalformedURLException;
22: import java.net.URISyntaxException;
23: import java.net.URL;
24: import java.nio.file.FileSystem;
25: import java.nio.file.FileSystems;
26: import java.nio.file.FileVisitResult;
27: import java.nio.file.Files;
28: import java.nio.file.Path;
29: import java.nio.file.Paths;
30: import java.nio.file.SimpleFileVisitor;
31: import java.nio.file.attribute.BasicFileAttributes;
32: import java.util.Arrays;
33: import java.util.Collection;
34: import java.util.Collections;
35: import java.util.Enumeration;
36: import java.util.HashSet;
37: import java.util.Set;
38: import java.util.logging.Level;
39: import java.util.logging.Logger;
40:
41: class HelidonResourceLoader implements ResourceLoader {
42:
43: private final String catalog;
44: private final boolean loadCustomSchemaEnabled;
45:
46: private static final String DD_DIR = DeploymentDescriptorParser.JAXWS_WSDL_DD_DIR + "/";
47:
48: private static final Logger LOGGER = Logger.getLogger(HelidonResourceLoader.class.getName());
49:
50: HelidonResourceLoader(String catalog, boolean loadCustomSchemaEnabled) {
51: this.catalog = catalog;
52: this.loadCustomSchemaEnabled = loadCustomSchemaEnabled;
53: }
54:
55: @Override
56: public URL getResource(String resource) throws MalformedURLException {
57:• String res = resource.startsWith("/") ? resource.substring(1) : resource;
58: return Thread.currentThread().getContextClassLoader().getResource(res);
59: }
60:
61: @Override
62: public URL getCatalogFile() throws MalformedURLException {
63: return getResource(catalog);
64: }
65:
66: @Override
67: public Set<String> getResourcePaths(String path) {
68: // should be always true, warn if not
69:• if (("/" + DD_DIR).equals(path)) {
70: Set<String> r = new HashSet<>();
71: try {
72: Collection<URL> resources;
73: String res = path.substring(1);
74:• if (loadCustomSchemaEnabled) {
75: resources = Collections.list(Thread.currentThread().getContextClassLoader().getResources(res));
76: } else {
77: resources = Arrays.asList(getResource(res));
78: }
79:• for (URL rootUrl : resources) {
80: loadResources(path, rootUrl, r);
81: }
82: return r;
83: } catch (IOException e) {
84: LOGGER.log(Level.FINE, null, e);
85: }
86: }
87: LOGGER.log(Level.WARNING, "Empty set for {0}", path);
88: return Collections.EMPTY_SET;
89: }
90:
91: private Set<String> loadResources(String path, URL rootUrl, Set<String> r) {
92: FileSystem jarFS = null;
93: try {
94:• if (rootUrl == null) {
95: // no wsdls found
96: return r;
97: }
98: Path wsdlDir = null;
99:• switch (rootUrl.getProtocol()) {
100: case "file":
101: wsdlDir = Paths.get(rootUrl.toURI());
102: break;
103: case "jar":
104: jarFS = FileSystems.newFileSystem(rootUrl.toURI(), Collections.emptyMap());
105: wsdlDir = jarFS.getPath(path);
106: break;
107: default:
108: LOGGER.log(Level.WARNING, "Unsupported protocol: {0}", rootUrl.getProtocol());
109: LOGGER.log(Level.WARNING, "Empty set for {0}", rootUrl);
110: return Collections.EMPTY_SET;
111: }
112:
113: //since we don't know exact file extension (can be .wsdl, .xml, .asmx,...)
114: //nor whether the file is used or not (we are not processing wsdl/xsd imports here)
115: //simply return all found files
116: Files.walkFileTree(wsdlDir, new SimpleFileVisitor<Path>() {
117:
118: @Override
119: public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
120: try {
121: String p = DD_DIR + Paths.get(rootUrl.toURI()).relativize(file).toString();
122: r.add(p);
123: } catch (URISyntaxException ex) {
124: LOGGER.log(Level.FINE, null, ex);
125: }
126: return FileVisitResult.CONTINUE;
127: }
128: });
129: } catch (IOException | URISyntaxException ex) {
130: LOGGER.log(Level.FINE, null, ex);
131: } finally {
132:• if (jarFS != null) {
133: try {
134: jarFS.close();
135: } catch (IOException ex) {
136: LOGGER.log(Level.FINE, null, ex);
137: }
138: }
139: }
140: return r;
141: }
142:
143: }