-
Notifications
You must be signed in to change notification settings - Fork 20
/
ResourceLoaderAdapter.java
266 lines (245 loc) · 8.08 KB
/
ResourceLoaderAdapter.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
/*
* 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.jboss.ws.common;
import static org.jboss.ws.common.Loggers.ROOT_LOGGER;
import static org.jboss.ws.common.Messages.MESSAGES;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.jboss.wsf.spi.deployment.UnifiedVirtualFile;
/**
* The default file adapter loads resources through an associated classloader.
* If no classload is set, the the thread context classloader will be used.
*
* @author Heiko.Braun@jboss.org
* @author alessio.soldano@jboss.com
* @since 25.01.2007
*/
public class ResourceLoaderAdapter implements UnifiedVirtualFile
{
private static final long serialVersionUID = -8567810932195204615L;
private final URL resourceURL;
private final ClassLoader loader;
private static final String jarFileSeparator = "/";
public ResourceLoaderAdapter()
{
this(SecurityActions.getContextClassLoader());
}
public ResourceLoaderAdapter(ClassLoader loader)
{
this.resourceURL = null;
this.loader = loader;
}
private ResourceLoaderAdapter(ClassLoader loader, URL resourceURL)
{
this.resourceURL = resourceURL;
this.loader = loader;
}
private UnifiedVirtualFile findChild(String resourcePath, boolean throwExceptionIfNotFound) throws IOException
{
URL resourceURL = null;
if (resourcePath != null)
{
// Try the child as URL
try
{
resourceURL = new URL(resourcePath);
}
catch (MalformedURLException ex)
{
// ignore
}
// Try the filename as File
if (resourceURL == null)
{
try
{
File file = new File(resourcePath);
if (file.exists())
resourceURL = file.toURI().toURL();
}
catch (MalformedURLException e)
{
// ignore
}
}
// Try the filename as Resource
if (resourceURL == null)
{
try
{
resourceURL = loader.getResource(resourcePath);
}
catch (Exception ex)
{
// ignore
}
}
}
if (resourceURL == null)
{
if (throwExceptionIfNotFound)
{
throw MESSAGES.cannotGetURLFor(resourcePath);
}
else
{
if (ROOT_LOGGER.isTraceEnabled()) ROOT_LOGGER.cannotGetURLFor(resourcePath);
return null;
}
}
return new ResourceLoaderAdapter(loader, resourceURL);
}
public UnifiedVirtualFile findChild(String child) throws IOException
{
return findChild(child, true);
}
public UnifiedVirtualFile findChildFailSafe(String child)
{
try
{
return findChild(child, false);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public URL toURL()
{
if (null == this.resourceURL)
throw MESSAGES.unifiedVirtualFileNotInitialized(loader);
return resourceURL;
}
public List<UnifiedVirtualFile> getChildren() throws IOException
{
if (null == this.resourceURL)
throw MESSAGES.unifiedVirtualFileNotInitialized(loader);
List<UnifiedVirtualFile> list = new LinkedList<UnifiedVirtualFile>();
if (resourceURL.getProtocol().equals("jar"))
{
String urlString = resourceURL.toExternalForm();
String jarRoot = urlString.substring(4, urlString.indexOf("ar!") + 2);
String path = urlString.contains("!") ? urlString.substring(urlString.lastIndexOf("!") + 2) : "";
if (path.endsWith(jarFileSeparator))
path = path.substring(0, path.lastIndexOf(jarFileSeparator));
try
{
//get the jar and open it
String folder = jarRoot.substring(5,jarRoot.lastIndexOf("/"));
String filename = jarRoot.substring(jarRoot.lastIndexOf("/")+1);
final File jar = new File(folder, filename);
PrivilegedAction<JarFile> action = new PrivilegedAction<JarFile>()
{
public JarFile run()
{
try
{
return new JarFile(jar);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
};
JarFile jarFile = AccessController.doPrivileged(action);
if (jar.canRead())
{
Enumeration<JarEntry> entries = jarFile.entries();
List<String> pathMatch = new LinkedList<String>();
List<String> finalMatch = new LinkedList<String>();
while (entries.hasMoreElements())
{
JarEntry entry = entries.nextElement();
String name = entry.getName();
//keep entries starting with the current resource path (exclude inner classes and the current file)
if (name.startsWith(path + jarFileSeparator) && (name.length() > path.length() + 1) && !name.contains("$"))
pathMatch.add(name.substring(path.length() + 1));
}
for (String s : pathMatch)
{
//do not go deeper than the current dir
if (!s.contains(jarFileSeparator) || s.indexOf(jarFileSeparator) == s.length() - 1)
finalMatch.add(s);
}
for (String s : finalMatch)
{
URL sUrl = new URL(urlString + jarFileSeparator + s);
list.add(new ResourceLoaderAdapter(loader, sUrl));
}
}
}
catch (Exception e)
{
ROOT_LOGGER.cannotGetChildrenForResource(e, resourceURL);
}
}
else //std file/dir
{
try
{
File file = new File(resourceURL.toURI());
if (file.exists() && file.isDirectory())
{
File[] files = file.listFiles();
if (files != null)
{
for (File f : files)
{
list.add(new ResourceLoaderAdapter(loader, f.toURI().toURL()));
}
}
}
}
catch (Exception e)
{
ROOT_LOGGER.cannotGetChildrenForResource(e, resourceURL);
}
}
return list;
}
public String getName()
{
if (null == this.resourceURL)
throw MESSAGES.unifiedVirtualFileNotInitialized(loader);
String name = null;
try
{
String filename = resourceURL.getFile();
File f = new File(filename);
name = f.getName();
if (f.isDirectory() || (resourceURL.getProtocol().equals("jar") && filename.endsWith(jarFileSeparator)))
name = name + jarFileSeparator;
}
catch (Exception e)
{
ROOT_LOGGER.cannotGetNameForResource(e, resourceURL);
}
return name;
}
}