Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safely convert URI to File #19

Merged
merged 2 commits into from
Nov 21, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions src/main/java/hudson/plugins/tfs/model/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,25 @@ static synchronized void ensureNativeLibrariesConfigured() {
final Class<TFSTeamProjectCollection> metaclass = TFSTeamProjectCollection.class;
final ProtectionDomain protectionDomain = metaclass.getProtectionDomain();
final CodeSource codeSource = protectionDomain.getCodeSource();
// TODO: codeSource could be null; what should we do, then?
if (codeSource == null) {
// TODO: log that we were unable to determine the codeSource
return;
}
final URL location = codeSource.getLocation();
URI locationUri = null;
try {
locationUri = location.toURI();
} catch (URISyntaxException e) {
// this shouldn't happen
// TODO: consider logging this situation if it ever happens
// inspired by http://hg.netbeans.org/main/file/default/openide.filesystems/src/org/openide/filesystems/FileUtil.java#l1992
final String u = location.toString();
URI locationUri;
if (u.startsWith("jar:file:") && u.endsWith("!/")) {
locationUri = URI.create(u.substring(4, u.length() - 2));
}
else if (u.startsWith("file:")) {
locationUri = URI.create(u);
}
else {
// TODO: log that we were unable to determine location from codeSource
return;
}
final String stringPathToJar = locationUri.getPath();
final File pathToJar = new File(stringPathToJar);
final File pathToJar = new File(locationUri);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just beware that there is no documented standard format for CodeSource.location. URLClassLoader actually accepts two formats: file:…/code.jar and jar:file:…/code.jar!/. The second one is more logical—code sources ought to be directory URLs to which you can append resource URIs, creating a URL such as jar:file:…/code.jar!/pkg/Some.class. But both forms are found “in the field”, so you should explicitly check for the one using the jar URL protocol and strip off the jar: prefix and !/ suffix before trying to call File.<init>(URI) (which will throw an IllegalArgumentException if given the jar-protocol form), though currently Jenkins uses AntClassLoader whose defineClassFromData uses the file-protocol form, or PluginFirstClassLoader whose addPathFiles does the same. See this code for example.

final File pathToLibFolder = pathToJar.getParentFile();
final File pathToNativeFolder = new File(pathToLibFolder, "native");
System.setProperty(nativeFolderPropertyName, pathToNativeFolder.toString());
Expand Down