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
|
/*******************************************************************************
* Copyright (c) 2006, 2011 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.osgi.internal.location;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.eclipse.osgi.internal.location.Locker.MockLocker;
/**
* @since 3.3
*/
public class LocationHelper {
public static final String PROP_OSGI_LOCKING = "osgi.locking"; //$NON-NLS-1$
/**
* Builds a URL with the given specification
* @param spec the URL specification
* @param trailingSlash flag to indicate a trailing slash on the spec
* @return a URL
*/
@SuppressWarnings("deprecation")
public static URL buildURL(String spec, boolean trailingSlash) {
if (spec == null)
return null;
if (File.separatorChar == '\\')
spec = spec.trim();
boolean isFile = spec.startsWith("file:"); //$NON-NLS-1$
try {
if (isFile)
return adjustTrailingSlash(new File(spec.substring(5)).toURL(), trailingSlash);
return new URL(spec);
} catch (MalformedURLException e) {
// if we failed and it is a file spec, there is nothing more we can do
// otherwise, try to make the spec into a file URL.
if (isFile)
return null;
try {
return adjustTrailingSlash(new File(spec).toURL(), trailingSlash);
} catch (MalformedURLException e1) {
return null;
}
}
}
private static URL adjustTrailingSlash(URL url, boolean trailingSlash) throws MalformedURLException {
String file = url.getFile();
if (trailingSlash == (file.endsWith("/"))) //$NON-NLS-1$
return url;
file = trailingSlash ? file + "/" : file.substring(0, file.length() - 1); //$NON-NLS-1$
return new URL(url.getProtocol(), url.getHost(), file);
}
public static Locker createLocker(File lock, String lockMode, boolean debug) {
if ("none".equals(lockMode)) //$NON-NLS-1$
return new MockLocker();
if ("java.io".equals(lockMode)) //$NON-NLS-1$
return new Locker_JavaIo(lock);
if ("java.nio".equals(lockMode)) { //$NON-NLS-1$
return new Locker_JavaNio(lock, debug);
}
// Backup case if an invalid value has been specified
return new Locker_JavaNio(lock, debug);
}
}
|