Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 73050d44f597d1e56250165cd9787c9f62a222e6 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2006, 2016 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.*;
import java.net.*;
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$
	public static final String LOCKING_NONE = "none"; //$NON-NLS-1$
	public static final String LOCKING_IO = "java.io"; //$NON-NLS-1$
	public static final String LOCKING_NIO = "java.nio"; //$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.getPath();
		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 (lockMode == null) {
			// try to get the lockMode from the system properties
			lockMode = System.getProperty(PROP_OSGI_LOCKING);
		}
		if (LOCKING_NONE.equals(lockMode)) {
			return new MockLocker();
		}
		if (LOCKING_IO.equals(lockMode)) {
			return new Locker_JavaIo(lock);
		}
		if (LOCKING_NIO.equals(lockMode)) {
			return new Locker_JavaNio(lock, debug);
		}

		//	Backup case if an invalid value has been specified
		return new Locker_JavaNio(lock, debug);
	}

	public static InputStream getStream(URL location) throws IOException {
		if ("file".equalsIgnoreCase(location.getProtocol())) { //$NON-NLS-1$
			// this is done to handle URLs with invalid syntax in the path
			File f = new File(location.getPath());
			if (f.exists()) {
				return new FileInputStream(f);
			}
		}
		return location.openStream();
	}

	public static URLConnection getConnection(URL url) throws IOException {
		if ("file".equalsIgnoreCase(url.getProtocol())) { //$NON-NLS-1$
			try {
				return url.openConnection();
			} catch (IllegalArgumentException e) {
				// this is done to handle URLs with invalid syntax in the path for URIs
				File f = new File(url.getPath());
				if (f.exists()) {
					return f.toURI().toURL().openConnection();
				}
			}
		}
		return url.openConnection();
	}

	public static File decodePath(File file) {
		// Pre-check if file exists, if not, and it contains escape characters,
		// try decoding the absolute path generated by makeAbsolute
		if (!file.exists() && (file.getPath().indexOf('%') >= 0 || file.getPath().indexOf('+') >= 0)) {
			String absolute = file.getAbsolutePath();
			String decodePath = LocationHelper.decode(absolute, true);
			File f = new File(decodePath);
			if (f.exists()) {
				return f;
			}
			decodePath = LocationHelper.decode(absolute, false);
			f = new File(decodePath);
			if (f.exists()) {
				return f;
			}
		}
		return file;
	}

	public static String decode(String urlString, boolean plusEncoded) {
		//first encode '+' characters, because URLDecoder incorrectly converts 
		//them to spaces on certain class library implementations.
		if (plusEncoded && urlString.indexOf('+') >= 0) {
			int len = urlString.length();
			StringBuffer buf = new StringBuffer(len);
			for (int i = 0; i < len; i++) {
				char c = urlString.charAt(i);
				if (c == '+')
					buf.append("%2B"); //$NON-NLS-1$
				else
					buf.append(c);
			}
			urlString = buf.toString();
		}
		try {
			return URLDecoder.decode(urlString, "UTF-8"); //$NON-NLS-1$
		} catch (UnsupportedEncodingException e) {
			// Tried but failed
			// TODO should we throw runtime exception here?
			return urlString;
		} catch (RuntimeException e) {
			// May have illegal characters for decoding
			return urlString;
		}
	}
}

Back to the top