package org.eclipse.core.internal.runtime;
import java.io.IOException;
import java.net.URL;
import org.eclipse.core.internal.boot.PlatformURLConnection;
import org.eclipse.core.internal.boot.PlatformURLHandler;
import org.eclipse.osgi.util.NLS;
import org.osgi.framework.Bundle;
public class PlatformURLPluginConnection extends PlatformURLConnection {
private static boolean isRegistered = false;
public static final String PLUGIN = "plugin";
public PlatformURLPluginConnection(URL url) {
super(url);
}
@Override
protected boolean allowCaching() {
return true;
}
public static Object[] parse(String spec, URL originalURL) throws IOException {
Object[] result = new Object[2];
if (spec.startsWith("/"))
spec = spec.substring(1);
if (!spec.startsWith(PLUGIN))
throw new IOException(NLS.bind(CommonMessages.url_badVariant, originalURL));
int ix = spec.indexOf('/', PLUGIN.length() + 1);
String ref = ix == -1 ? spec.substring(PLUGIN.length() + 1) : spec.substring(PLUGIN.length() + 1, ix);
String id = getId(ref);
Activator activator = Activator.getDefault();
if (activator == null)
throw new IOException(CommonMessages.activator_not_available);
Bundle bundle = activator.getBundle(id);
if (bundle == null)
throw new IOException(NLS.bind(CommonMessages.url_resolvePlugin, id));
result[0] = bundle;
result[1] = (ix == -1 || (ix + 1) >= spec.length()) ? "/" : spec.substring(ix + 1);
return result;
}
@Override
protected URL resolve() throws IOException {
String spec = url.getFile().trim();
Object[] obj = parse(spec, url);
Bundle b = (Bundle) obj[0];
String path = (String) obj[1];
URL result = b.getEntry(path);
if (result != null || "/".equals(path))
return result;
result = b.getResource(path);
if (result != null)
return result;
return new URL(b.getEntry("/"), path);
}
public static void startup() {
if (isRegistered)
return;
PlatformURLHandler.register(PLUGIN, PlatformURLPluginConnection.class);
isRegistered = true;
}
}